//
//  COGiftCollectionListCell.swift
//  CocoProject
//
//  Created by Vill on 2023/11/14.
//

import UIKit
import QMUIKit

class COGiftCollectionListCell: UICollectionViewCell {
    
    var giftModel: COGiftModel? {
        didSet {
           setContent()
        }
    }

    override var isSelected: Bool {
        didSet{
            if self.isSelected == true{
                self.bgContain.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF",0.3).cgColor,UIColor.colorWithHex("#FF3DE1",0.3).cgColor], direction: .Horizontal, range: (SCREENWIDTH - PTW(25))/4)
                self.bgContain.clipsAllCorners(radius: PTW(20))
            }else{
                self.bgContain.backgroundColor = UIColor.clear
            }
        }
    }
    
    lazy var bgContain: UIView = {
        let bg = UIView()
        return bg
    }()
    
    lazy var giftImg :UIImageView = {
        let gift = UIImageView()
        gift.contentMode = .scaleAspectFit
        gift.layer.masksToBounds = true
        return gift
    }()
    
//    lazy var giftName:UILabel = {
//        let name = UILabel()
//        name.textColor = UIColor.colorWithHex("#2C255B")
//        name.font = UIFont.pingfangFont(ofSize: PTW(14), weight: .medium)
//        name.textAlignment = .center
//        name.numberOfLines = 0
//        return name
//    }()
    
    lazy var giftName:QMUIMarqueeLabel = {
        let name = QMUIMarqueeLabel()
        name.spacingBetweenHeadToTail = 5
        name.fadeWidthPercent = 0
        name.textColor = UIColor.colorWithHex("#2C255B")
        name.font = UIFont.pingfangFont(ofSize: PTW(14), weight: .medium)
        name.textAlignment = .center
        name.numberOfLines = 0
        return name
    }()
    
    lazy var priceLbl:UILabel = {
        let name = UILabel()
        name.textColor = UIColor.colorWithHex("#7863A6")
        name.font =  UIFont.pingfangFont(ofSize: PTW(12), weight: .medium)
        return name
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        contentView.addSubview(bgContain)
        contentView.addSubview(giftImg)
        contentView.addSubview(giftName)
        contentView.addSubview(priceLbl)
        bgContain.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        giftImg.snp.makeConstraints { make in
            make.size.equalTo(PTW(70))
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(PTW(5))
        }
        giftName.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(giftImg.snp.bottom).offset(PTW(4))
            make.height.equalTo(PTW(17))
        }
        priceLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(giftName.snp.bottom)
        }
    }
    
    func setContent(){
        guard giftModel != nil else {
            return
        }
        giftImg.sd_setUrlImage(giftModel?.iconUrl, placeholder: KImaged("coco_mine_star"))
        giftName.text = giftModel?.name
        self.priceLbl.attributedText = "\(giftModel?.coins ?? 0)".stringWithImage(img: "coco_mine_star", bounds: CGRect(x: 0, y: -PTW(2) , width: PTW(12) , height: PTW(12)), isFirst: true)
    }
}
